package com.dcy.daily.skill.springboot.readYml;

/**
 * @author duanchunyu
 * @version 1.0.0
 * @Description 读取yml配置信息的测试类
 * @createTime 2021年05月28日 10:19:00
 */

import com.dcy.daily.common.config.annotation.ComponentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

import java.util.Arrays;

//将有@ConfigurationProperties注解的类（targetClass）加入到IOC容器中。
@ComponentUtil.CComponent
@EnableConfigurationProperties(UserInfoOfProperties.class)
public class UsageYml implements ApplicationRunner {

    @Autowired
    private UserInfoOfValue userInfoOfValue;

    @Autowired
    private UserInfoOfProperties userInfoOfProperties;

    @Autowired
    private UserInfo userInfo;

    /**
     * 使用@Value注解来读取yml
     */
    public void userInfoOfValue(){
        System.out.println("获取application.yml配置文件中的信息：");
        System.out.println("用户编号：" + userInfoOfValue.getUserId());
        System.out.println("用户名称：" + userInfoOfValue.getUserName());
        System.out.println("博客地址：" + userInfoOfValue.getBlogUrl());
        System.out.println("兴趣爱好：共" + userInfoOfValue.getHobbys().length + "个，" + Arrays.toString(userInfoOfValue.getHobbys()));
        System.out.println("备注信息：" + userInfoOfValue.getBlogRemark());
    }

    /**
     * 使用ConfigurationProperties读取配置信息
     */
    public void userInfoOfProperties()
    {
        System.out.println("获取application.yml配置文件中的信息：");
        System.out.println("用户编号：" + userInfoOfProperties.getUserId());
        System.out.println("用户名称：" + userInfoOfProperties.getUserName());
        System.out.println("博客地址：" + userInfoOfProperties.getBlogUrl());
        System.out.println("兴趣爱好：共" + userInfoOfProperties.getHobbys().length + "个，" + Arrays.toString(userInfoOfProperties.getHobbys()));
        System.out.println("备注信息：" + userInfoOfProperties.getBlogRemark());
    }

    /**
     * 常用配置形式
     */
    public void userInfo(){
        System.out.println("获取application.yml配置文件中的信息：");
        System.out.println("用户编号：" + userInfo.getUserId());
        System.out.println("用户名称：" + userInfo.getUserName());
        System.out.println("博客地址：" + userInfo.getBlogUrl());
        System.out.println("兴趣爱好：共" + userInfo.getHobbys().length + "个，" + Arrays.toString(userInfo.getHobbys()));
        System.out.println("备注信息：" + userInfo.getBlogRemark());
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        userInfoOfValue();
    }
}
